package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.TMenuDao;
import com.ktjy.ktmall.entity.Menu;
import com.ktjy.ktmall.entity.OrderDetail;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TMenuDaoImpl extends BaseDao implements TMenuDao {
    @Override
    public List<Menu> getMenuList() {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        List<Menu> list = new ArrayList<>();
        String sql = "select * from t_menu";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                Menu menu=new Menu();
                menu.setPs_id(rs.getInt("ps_id"));
                menu.setPs_name(rs.getString("ps_name"));
                menu.setPs_pid(rs.getInt("ps_pid"));
                menu.setPs_level(rs.getInt("ps_level"));
                menu.setPs_api_path(rs.getString("ps_api_path"));
                menu.setPs_api_order(rs.getInt("ps_api_order"));
                menu.setPs_icon(rs.getString("ps_icon"));
                menu.setEnabled(rs.getInt("enabled"));
                menu.setCreate_time(rs.getString("create_time"));
                menu.setUpdate_time(rs.getString("update_time"));
                list.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    @Override
    public List<Menu> getMenusByIds(int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = this.getConnection();
        List<Menu> list = new ArrayList<>();
        String sql = "select * from t_menu where ps_pid = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setObject(1, id);
            rs = pstmt.executeQuery();
            while (!rs.isClosed() && rs.next()) {
                Menu menu=new Menu();
                menu.setPs_id(rs.getInt("ps_id"));
                menu.setPs_name(rs.getString("ps_name"));
                menu.setPs_pid(rs.getInt("ps_pid"));
                menu.setPs_level(rs.getInt("ps_level"));
                menu.setPs_api_path(rs.getString("ps_api_path"));
                menu.setPs_api_order(rs.getInt("ps_api_order"));
                menu.setPs_icon(rs.getString("ps_icon"));
                menu.setEnabled(rs.getInt("enabled"));
                menu.setCreate_time(rs.getString("create_time"));
                menu.setUpdate_time(rs.getString("update_time"));
                list.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }
}
